﻿/*
 * Created by SharpDevelop.
 * User: notmmao
 * Date: 2008-3-29
 * Time: 4:05
 * 
 */

using System;
using System.Collections.Generic;
using System.Drawing;

namespace Deebug.Games.ShortBoom
{
	/// <summary>
	/// 游戏关卡类，保存有敌人和万家数量、初始位置、运动方向的信息.
	/// 同时也包好一些游戏逻辑，如相应控制输入和检查Sprites之间的碰撞
	/// </summary>
	public class Gate:GameObject
	{
		#region 字段
		System.ComponentModel.ComponentResourceManager resources
			= new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
		
		int _currentFrame = 0;
		List<Bitmap> _frames = new List<Bitmap>();
		
		List<GameObject> _gameObjects = new List<GameObject>();
		List<Alien> _aliens = new List<Alien>();
		Player _player ;
		Ball _ball ;
		Scene _scene;
		
		GameStates currentState = GameStates.Pausing;
		
		DBGraphics _dbg;
		
		Brush brush = Brushes.Blue;
		Font font = new Font("Tempus Sans ITC", 20,
		                     FontStyle.Bold, GraphicsUnit.Point);
		Pen pen = new Pen(Brushes.Blue,3);
		
		string _leftText;
		string _rightText;
		int FSP;
		#endregion
		
		#region 属性
		/// <summary>
		/// 玩家
		/// </summary>
		public Player Player {
			get {
				return _player;
			}
		}
		/// <summary>
		/// 敌人队列
		/// </summary>
		public List<Alien> Aliens {
			get {
				return _aliens;
			}
		}
		/// <summary>
		/// 关卡背景
		/// </summary>
		public Bitmap BackImage {
			get {
				return _frames[_currentFrame];
			}
		}
		
		/// <summary>
		/// 关卡大小
		/// </summary>
		public Size Size{
			get{
				return  _frames[_currentFrame].Size;
			}
		}
		/// <summary>
		/// 关卡宽
		/// </summary>
		public int Width{
			get{
				return Size.Width;
			}
		}
		/// <summary>
		/// 关卡高
		/// </summary>
		public int Height{
			get{
				return Size.Height;
			}
		}
		/// <summary>
		/// 当前状态
		/// </summary>
		public GameStates CurrentState {
			get {
				return currentState;
			}
			set {
				currentState = value;
			}
		}
		#endregion
		
		/// <summary>
		/// 为方便开发中的调试，而设计得默认构造函数。
		/// 负责初始化默认关卡中的一些数据
		/// </summary>
		/// <param name="scene">该关卡所必需的Scene舞台</param>
		public Gate(Scene scene)
		{
			_scene = scene;
			Bitmap bj = (Bitmap)resources.GetObject("bj");
			_frames.Add(bj);
			_dbg = new DBGraphics(BackImage);
			Initialize();
		}
		/// <summary>
		/// 主构造函数
		/// </summary>
		/// <param name="scene">游戏运行场景</param>
		/// <param name="frames">关卡背景图片列表</param>
		/// <param name="aliens">敌人队列</param>
		/// <param name="player">玩家</param>
		/// <param name="ball">子弹</param>
		public Gate(Scene scene,List<Bitmap> frames, List<Alien> aliens, Player player, Ball ball)
		{
			this._scene = scene;
			this._frames = frames;
			this._aliens = aliens;
			this._player = player;
			this._ball = ball;
			InitSprites();
		}
		
		#region 初始化GameObject
		
		public void Initialize(){
			this._gameObjects.Clear();
			this._aliens.Clear();
			DefaultGate();
			InitSprites();
		}
		
		private void InitSprites(){
			
			this._gameObjects.Add(_player);
			this._gameObjects.Add(_ball);
			//TODO:尝试换用_sprites.AddRange(_aliens);
			foreach(Alien s in _aliens){
				this._gameObjects.Add(s);
			}
		}
		//默认调试关卡信息
		private void DefaultGate(){
			
			Bitmap skin = (Bitmap)resources.GetObject("player");
			Vector alienVector = new Vector(1,1);

			Point playerLocation = new Point((this.BackImage.Width-skin.Width)/2,
			                                 (this.BackImage.Height-skin.Height));
			
			Alien p1 = new Alien(this,0,10,skin,alienVector,10);
			Alien p2 = new Alien(this,100,10,skin,alienVector,10);
			Alien p3 = new Alien(this,200,10,skin,alienVector,10);
			Alien p4 = new Alien(this,300,10,skin,alienVector,10);
			Alien p5 = new Alien(this,400,10,skin,alienVector,10);
			Alien p6 = new Alien(this,500,10,skin,alienVector,10);

			_player = new Player(this,playerLocation,skin);
			
			_aliens.Add(p1);
			_aliens.Add(p2);
			_aliens.Add(p3);
			_aliens.Add(p4);
			_aliens.Add(p5);
			_aliens.Add(p6);
			
			_ball = new Ball(this,skin,_player);
		}

		#endregion
		
		#region 检查碰撞

		//检查敌人是否与玩家碰撞
		private void AlienCollision(){
			foreach(Alien a in this._aliens){
				if(Sprite.Collision(a,this._player)){
					currentState = GameStates.GameOver;
					return;//跳出循环
				}
			}
		}
		//检查敌人是否与子弹碰撞
		private void BallCollision(){
			if(this._ball.Visible){
				foreach(Alien a in this._aliens){
					if(Sprite.Collision(a,this._ball)){
						this._gameObjects.Remove(a);
						this._ball.Visible = false;
						return;//跳出循环
					}
				}
			}
		}
		#endregion

		/// <summary>
		/// 关卡绘制
		/// 
		/// </summary>
		/// <param name="graphics">图形设备对象</param>
		public override void Render(Graphics graphics)
		{
			foreach(GameObject go in this._gameObjects){
				
				go.Render(_dbg.BackGroundGraphics);
			}
			
			_dbg.BackGroundGraphics.DrawString(this._leftText,font,brush,0,0);
			_dbg.BackGroundGraphics.DrawString(this._rightText,font,brush,Width-8*40,0);
			
			if(this.currentState == GameStates.Pausing)
				_dbg.BackGroundGraphics.DrawString("PAUSE!",font,brush,300,260);
			
			if(this.currentState == GameStates.GameOver)
				_dbg.BackGroundGraphics.DrawString("GAME OVER!",font,brush,300,260);
			
			_dbg.Render(graphics);

		}
		
		/// <summary>
		/// 游戏更新
		/// </summary>
		/// <param name="gameTime">当前游戏时间</param>
		/// <param name="elapsedTime">据上次更新所逝去的时间</param>
		public override void UpDate(float gameTime, float elapsedTime)
		{
			if(this.currentState != GameStates.Playing)
				return;
			if(elapsedTime != 0)
				FSP  =  (int)(1.0 /elapsedTime);
			_leftText = "Current FSP："+FSP;
			_rightText = "Game Time:" + gameTime;
			
			foreach(GameObject go in this._gameObjects){
				go.UpDate(gameTime,elapsedTime);
			}
			
			AlienCollision();
			BallCollision();
		}

		
	}
	
	public enum GameStates{
		Playing,
		Pausing,
		GameOver,
	}
}
